package com.yunji.web.system;

import com.google.common.collect.Maps;
import com.yunji.base.cache.BaseCache;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.model.domain.SysMenuDO;
import com.yunji.common.model.domain.SysUserDO;
import com.yunji.common.model.dto.LoginBody;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.model.dto.SmsLoginBody;
import com.yunji.common.model.vo.RouterVo;
import com.yunji.common.utils.SecurityUtils;
import com.yunji.system.service.ISysMenuService;
import com.yunji.system.service.SysLoginService;
import com.yunji.system.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.yunji.common.constant.General.TOKEN;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "登陆验证")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private BaseCache baseCache;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登陆方法")
    @PostMapping("/login")
    public Response<Map<String, Object>> login(@RequestBody LoginBody loginBody) {
        Map<String, Object> map = Maps.newHashMap();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        map.put(TOKEN, token);
        return ResponseHelper.success(map);
    }

    @ApiOperation("短信登录")
    @PostMapping("/smsLogin")
    public Response<Map<String, Object>> smsLogin(@RequestBody SmsLoginBody smsLoginBody) {
        Map<String, Object> map = Maps.newHashMap();
        String token = loginService.smsLogin(smsLoginBody);
        map.put(TOKEN, token);
        return ResponseHelper.success(map);
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public Response<Map<String, Object>> getInfo() {
        LoginInfo info = SecurityUtils.getLoginUser();
        SysUserDO user = BaseCache.getUser(info.getUserId());
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        Map<String, Object> map = Maps.newHashMap();
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        return ResponseHelper.success(map);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public Response<List<RouterVo>> getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenuDO> menus = menuService.selectMenuTreeByUserId(userId);
        return ResponseHelper.success(menuService.buildMenus(menus));
    }

    @ApiOperation("获取某个用户token")
    @GetMapping("/getToken")
    public Response<Boolean> getToken() {
        return ResponseHelper.success();
    }
}
